#include<set>
#include<vector>
#include<queue>
using namespace std;
class Solution {
public:
    int lastStoneWeight(vector<int>& stones) {
        multiset<int,greater<int>>m;
        for(auto&e:stones)m.insert(e);
        while(m.size()>1){
            auto a=m.begin();
            auto b=a;
            b++;
            int n=*a-*b;
            m.erase(a),m.erase(b);
            if(n)m.insert(n);
        }
        return m.size()?*m.begin():0;
    }
};
class Solution {
public:
    int lastStoneWeight(vector<int>& stones) {
        priority_queue<int>heap;
        for(auto&e:stones)heap.push(e);
        while(heap.size()>=2){
            int a=heap.top();
            heap.pop();
            int b=heap.top();
            heap.pop();
            int n=a-b;
            if(n) heap.push(n);
        }
        return heap.empty()?0:heap.top();
    }
};